package controller;

import java.sql.ResultSet;
import java.util.LinkedList;
import misc.Configurations;
import misc.Logger;
import data.DatabaseGateway;
import data.File;
import data.UploadLog;

public class ManageController {
    public LinkedList<UploadLog> retrieveUploadLog(String username){
        try{
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                    Configurations.DatabaseName);

            String query="SELECT *,FILES.Size as 'filesize',"+
                    "FILES.Type as'filetype',"+
                    "FILES.isProtected as 'fileProtected',"+
                    "FILES.OriginalName as 'filename' FROM UPLOADLOGS,"+
                    "FILES WHERE UPLOADLOGS.FileID=FILES.FileID AND "+
                    "FILES.isDeleted=false AND "+
                    "UPLOADLOGS.Username='"+username+"'";
            
            //Querying
            ResultSet rs_manage = data.executeQuery(query);

            //Convert results to interactable items
            LinkedList<UploadLog> manage = new LinkedList<UploadLog>();
            while(rs_manage.next())
            {
                UploadLog new_upload = new UploadLog();
                new_upload.setID(rs_manage.getLong("ID"));
                new_upload.setFileID(rs_manage.getLong("FileID"));
                new_upload.setFileName(rs_manage.getString("OriginalName"));
                new_upload.setFilesize(rs_manage.getLong("filesize"));
                new_upload.setFiletype(rs_manage.getString("filetype"));
                new_upload.setIPaddress(rs_manage.getString("IP"));
                new_upload.setStart(rs_manage.getTimestamp("StartTimeDate"));
                new_upload.setFinish(rs_manage.getTimestamp("FinishTimeDate"));
                new_upload.setFileIsProtected(rs_manage.
                                                getBoolean("fileProtected"));
                manage.add(new_upload);
            }

            data.closeConnection();
            return manage;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }

    public File retrieveFile(long fileID)
    {
        try
        {
            String query = "SELECT * FROM FILES WHERE FileID='"+fileID+"'";

            DatabaseGateway gw = new DatabaseGateway();
            gw.openConnection(Configurations.User,Configurations.Password,
                                                Configurations.DatabaseName);
            int numRows=gw.executeCount(query);

            if(numRows<=0)
                return null;

            ResultSet rs = gw.executeQuery(query);

            File new_file = new File();
            rs.next();
            new_file.setHashedCode(rs.getString("HashedCode"));
            gw.closeConnection();

            return new_file;
        }
        catch(Exception e)
        {
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }
}
